import java.util.ArrayList;
import java.util.List;

public class BinaryTreePaths {

    List<String> ans = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        process(root);
        return ans;
    }

    public void process(TreeNode node) {
        if (node == null) return;
        list.add(node.val);
        if (node.left == null && node.right == null) {
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < list.size() - 1; i++) {
                stringBuffer.append(list.get(i) + "->");
            }
            stringBuffer.append(list.get(list.size() - 1));
            ans.add(stringBuffer.toString());
        }
        process(node.left);
        process(node.right);
        list.remove(list.size() - 1);
    }
}
